package com.ironone.streaming;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SlidingPaneLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.ironone.streaming.application.CusAlrtBtnTypes;
import com.ironone.streaming.application.CusAlrtIconTypes;
import com.ironone.streaming.application.CustomAlertDialog;
import com.ironone.streaming.application.FormObject;
import com.ironone.streaming.lite.R;

public class AlbumArtFragment extends Fragment {

	View view;
	SlidingPaneLayout mPane;
	ImageView heading_logo;
	
	Button btnMenu;
	ImageView albumart_bg;
	
	ImageView retryButton_circle;
	ImageButton retryButton_flame;
	Animation rotation;
	LinearLayout loading_wrapper;
	
	ImageView logo;

	@Override
	public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.albumart_fragment, container, false);

		try {
			mPane = (SlidingPaneLayout) getActivity().findViewById(R.id.pane);
			heading_logo = (ImageView) view.findViewById(R.id.heading_logo);
			btnMenu = (Button) view.findViewById(R.id.btnMenu); 

			//Menu btn
			int supWidth = (int)((FormObject.getInstance().getScreenWidth() * 0.6));
			RelativeLayout.LayoutParams abParam = (RelativeLayout.LayoutParams) btnMenu.getLayoutParams();
			abParam.width = (int)(supWidth - (supWidth * 0.88));
			abParam.height = (int)(supWidth - (supWidth * 0.88));
			btnMenu.setLayoutParams(abParam);
			
			btnMenu.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {

					/*btnMenu.setClickable(false);
					new Handler().postDelayed(new Runnable() {

						@Override
						public void run() {
							btnMenu.setClickable(true);
						}
					}, 1000);
					 */
					mPane.openPane();
				}
			});
			
			//Background overlay image
			//bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg_frag);  //Default image
			/*albumart_bg = (ImageView) view.findViewById(R.id.albumart_bg);
			ViewTreeObserver vto = albumart_bg.getViewTreeObserver();
			vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
				@Override
				public void onGlobalLayout() {
					albumart_bg = (ImageView) view.findViewById(R.id.albumart_bg);
					if(FormObject.getInstance().getAlbumArtImage() != null && MyMediaPlayer.isPlaying()){
						//albumart_bg.setImageBitmap(bitmap);
						//albumart_bg.setAlpha(64);
						albumart_bg.setImageBitmap(MainApplication.getByteArrToBitmap(FormObject.getInstance().getAlbumArtImage()));
					}
					//else 
						//albumart_bg.setImageBitmap(FormObject.getInstance().getAlbumArtImage());
				}
			});*/
			
			RelativeLayout.LayoutParams aaParam = (RelativeLayout.LayoutParams) heading_logo.getLayoutParams();
			aaParam.width = supWidth;
			heading_logo.setLayoutParams(aaParam);
			
			//Logo
			logo=(ImageView) view.findViewById(R.id.imageView_clientlogo);
			RelativeLayout.LayoutParams logoParam = (RelativeLayout.LayoutParams) logo.getLayoutParams();
			logoParam.width = (int) (supWidth - (supWidth * 0.62));
			logoParam.height = (int) (supWidth - (supWidth * 0.62));
			logo.setLayoutParams(logoParam);
			
			//Loading icon
			retryButton_flame = (ImageButton) view.findViewById(R.id.retryButton_flame);
			retryButton_circle = (ImageButton) view.findViewById(R.id.retryButton_circle);
			loading_wrapper = (LinearLayout) view.findViewById(R.id.loading_wrapper);
			//errorTextView = (TextView) view.findViewById(R.id.errorTextView);
			
			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) retryButton_flame.getLayoutParams();
			params.width = (int) (FormObject.getInstance().getScreenWidth() / 10);
			params.height = params.width;
			retryButton_flame.setLayoutParams(params);
			retryButton_circle.setLayoutParams(params);

			retryButton_circle = (ImageView) view.findViewById(R.id.retryButton_circle);
			rotation = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_button);
			rotation.setRepeatCount(Animation.INFINITE);

			if(!FormObject.getInstance().getIsOneTimeExecuted()){
				retryButton_circle.startAnimation(rotation);
			}else{
				FormObject.getInstance().setIsSlidingPaneLayoutSlidable(true);
				loading_wrapper.setVisibility(View.GONE);
				btnMenu.setVisibility(View.VISIBLE);
			}
		
			/*ViewTreeObserver vto = retryButton_flame.getViewTreeObserver();
			vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
				@Override
				public void onGlobalLayout() {
					
				}
			});*/

		} catch (Exception e) {
			CustomAlertDialog alertDialog = new CustomAlertDialog(getActivity(), CusAlrtBtnTypes.OK, CusAlrtIconTypes.DUCK);
			alertDialog.setTitleText(getString(R.string.what_the_duck_));
			//alertDialog.setMessageText(getString(R.string.something_snapped));			
			alertDialog.show();
		}		
		return view;
	}
}
